/**加载 js 
 * 依附js里的data 必须有loadingJS:require('/ui/loadingPage.js') */
var test = 'loadingPage';
//请求业务处理成功
var isSucceed=false;
//显示加载
var isShow = true;
//显示空数据
var isShowEmpty = false;
//显示加载是失败
var isShowFail = false;
var loadingEmptyImg = "/images/loading_empty.png";
var loadingFailImg = "/images/loading_failure.png";
 
//开始显示加载
function loadingShow(dependView) {
  this.isShow = true;
  this.isSucceed=false;
  this.isShowEmpty = false;
  this.isShowFail = false;
  dependView.setData({ loadingJS: this });
  this.timingTask(this);
};
//显示空数据图
function loadingEmpty(dependView, failImgImage) {
  this.isShowEmpty = true;
  this.isShowFail = false;
  this.isShow = false;
  if (failImgImage != null) {
    this.loadingFailImg = failImgImage;
  }
  //重新设置loadingJS值，loadingPage.wxml才能更新页面
   dependView.setData({ loadingJS: this });
};
//网络请求失败
function loadingFail(dependView, emptyImage) {
  this.isShowFail = true;
  this.isShowEmpty = false;
  this.isShow = false;
  if (emptyImage != null) {
    this.loadingEmptyImg = emptyImage;
  }
 dependView.setData({ loadingJS: this });
};
//加载成功
function loadingSucceed(dependView){
  if (this.isSucceed){
    return;
  }
  this.isSucceed = true;
  this.isShowFail = false;
  this.isShowEmpty = false;
  this.isShow = false;
  dependView.setData({ loadingJS: this });
};
//定时任务
/**
 * dependView:依附view 的js
 * loadingSelf：本js
 */
function timingTask(loadingSelf) {
  if (!this.isShow) {
    console.log("loading加载结束");
    return;
  }
  setTimeout(function () {
    loadingSelf.tailorImg();
    loadingSelf.timingTask(loadingSelf);
  }, 1 * 100)
};
//裁剪高度 
var loadingRate = 120;
//图片裁剪范围{ 42-120}
function tailorImg() {
  var src = "/images/base_loading_up_false.png";
  var ctx = wx.createCanvasContext('cutImg1');
  var temp = loadingRate;
  if (loadingRate > 42) {
    loadingRate -= 5;
  }
  if (temp > 42 && loadingRate < 42) {
    loadingRate = 42;
  }
  if (temp == 42) {
    loadingRate = 120;
  }
  ctx.restore();
  ctx.save();
  ctx.setFillStyle('rgba(0, 0, 0, 0)');
  ctx.fillRect(0, 0, 160, loadingRate);
  ctx.clip();
  ctx.drawImage(src, 0, 0, 160, 160);
  ctx.draw();
};
//暴露出去，其它页面才能用
module.exports = {
  //测试
 
  test,
  //
  isShowEmpty,
  loadingEmptyImg,
  isShowFail,
  loadingFailImg,
  isSucceed,
  //
  loadingShow,
  timingTask,
  tailorImg,
  loadingEmpty,
  loadingFail,
  loadingSucceed,
}
